<?php
class table {
	public $t_head = array();
	public $t_content = array();
	public $t_features = array('sPaginationType' => '"full_numbers"');
	public $t_id = "table";

	function set_head($head_array){
		$this->t_head=$head_array;
	}

	function add_row($row_array){
		if (count($row_array) == count($this->t_head)) {
			$this->t_content[]=$row_array;
			return true;
		} else {
			return false;
		}
	}
	
	function add_feature($name, $value){
		$this->t_features[$name] = $value;
	}

	function script(){
		$result = "$('#" . $this->t_id . "').dataTable({ \"bJQueryUI\": true";
		foreach ($this->t_features as $name => $value){
			$result .= ', "' . $name . '": ' . $value ;
		}
		$result .= "});";

		return $result;
	}

	function html(){
		$a = "";
		$a .= "\n" . '<table cellpadding="0" cellspacing="0" border="0" class="display" id="' . $this->t_id . '">' . "\n";
		$a .= "\t" . '<thead>' . "\n";
		$a .= "\t\t" . '<tr>' . "\n";
		for ($i = 0; $i < count($this->t_head); $i++){
			$a .= "\t\t\t" . '<th>' . $this->t_head[$i] . '</th>' . "\n";
		}
		$a .= "\t\t" . '</tr>' . "\n";
		$a .= "\t" . '</thead>' . "\n";
		$a .= "\t" . '<tbody>' . "\n";
		for ($i = 0; $i < count($this->t_content); $i++){
			$a .= "\t\t" . '<tr class="">' . "\n";
			for ($j = 0; $j < count($this->t_content[$i]); $j++){
				$a .= "\t\t\t" . '<td>' . $this->t_content[$i][$j] . '</td>' . "\n";
			}
			$a .= "\t\t" . '</tr>'. "\n";
		}
		$a .= "\t" . '</tbody>' . "\n";
		$a .= "\t" . '<tfoot>' . "\n";
		$a .= "\t\t" . '<tr>' . "\n";
		for ($i = 0; $i < count($this->t_head); $i++){
			$a .= "\t\t\t" . '<th>' . $this->t_head[$i] . '</th>' . "\n";
		}
		$a .= "\t\t" . '</tr>' . "\n";
		$a .= "\t" . '</tfoot>' . "\n";
		$a .= '</table>';

		return $a;	
	}

}
/*
$a = new table();
$a->set_head(array('Id', 'Nombre', 'Valor'));
$a->add_row(array(1,'nombre1','valor1'));
$a->add_row(array(2,'nombre2','valor2'));
$a->add_row(array(3,'nombre3','valor3'));
$a->add_row(array(4,'nombre4','valor4'));

print $a->html();
*/
?>
